package top.dbpanda.cmsjpa.learn.thread.juc;

import lombok.AllArgsConstructor;

import java.util.concurrent.CountDownLatch;

/**
 * @author 20018954
 */
public class TestCountDownLatch {
    public static void main(String[] args) {

        CountDownLatch countDownLatch = new CountDownLatch(10);
        MyTread myTread = new MyTread(countDownLatch);
        long start = System.currentTimeMillis();
        for (int i = 0; i < 10; i++) {
            new Thread(myTread).start();
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("耗时：" + (end - start));

    }
}

@AllArgsConstructor
class MyTread implements Runnable {
    private CountDownLatch latch;

    @Override
    public void run() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            //计数器减一
            latch.countDown();
        }
    }
}
